package com.lyd.basic;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @Author:lyd
 **/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Cloneable {
    private String name;
    private int age;
    private Address addressName;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 省略构造函数、Getter&Setter方法

    //浅拷贝
//    @Override
//    public Person clone() {
//        try {
//            Person person = (Person) super.clone();
//            return person;
//        } catch (CloneNotSupportedException e) {
//            throw new AssertionError();
//        }
//    }

    //深拷贝
    @Override
    public Person clone() {
        try {
            //先克隆出一个学生对象
            Person person = (Person) super.clone();
            //调用Address中的克隆方法，克隆出一个Address对象
            Address address = person.getAddressName().clone();
//            Address address = this.addressName.clone();
            //将克隆出的Address复制给Person
            person.setAddressName(address);
            return person;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", addressName=" + addressName +
                '}';
    }
}
